<?php


require_once('DATA.php');

/**
 * Clase que maneja la conexion a la base de datos.
 * @author rodolfoflorentin
 * @version 10-2011
 */
class DbConnection
{
	private $conn;
	private $result;
	private $countRow;
	
	/**
         *
         */
	function  __construct()
        {
			$data = new DATA();
            $this->conn = mysql_connect($data->DB_HOST,$data->DB_USER,$data->DB_PW) or die (mysql_error($this->conn));
            mysql_select_db($data->DB_NAME, $this->conn) or die (mysql_error($this->conn));
            //Esta funcion es necesaria para que se ejecute el destructor al finalizar el script.
            register_shutdown_function(array(&$this, "_DbConnection"));
	}
	/**
         *
         */
	function  _DbConnection() 
        {
            mysql_close($this->conn);
	}
	/**
         *
         * @param <type> $sqlQuery
         * @return <type> 
         */
	function makeQuery($sqlQuery) 
        {
            if (is_string($sqlQuery)) {
                    $this->result = mysql_query($sqlQuery, $this->conn) or die ("ERROR<BR><BR>" . mysql_error($this->conn).$sqlQuery);
            }
            return $this->result;
	}
        /**
         *
         * @return 
         */
	function nextRow() 
        {
            $row = mysql_fetch_assoc($this->result);
            if ($row) {
                    return $row;
            }
            return false;
	}

        /**
         *
         * @return
         */
	function nextRowObject($class_name)
        {
            $row = mysql_fetch_object($this->result, $class_name);
            if ($row) {                    
                    return $row;

            }
            return false;
	}
	/**
         *
         * @return <type> 
         */
	function getRowsCount() 
        {
            $this->countRow = mysql_num_rows($this->result);
            if (isset($this->countRow) and is_int($this->countRow)) {
                    return $this->countRow;
            }
            return;
	}
	/**
         * Retorna el id del ultimo registro ingresado.
         * @return int El id del ultimo registro ingresado.
         */
	function getInsertedId()
        {
            return mysql_insert_id();
	}
			

	/**
         *
         * @param <type> $fieldName
         * @return <type> 
         */
	function getFieldArray($fieldName) 
        {
            $resultArray = array($this->getRowsCount());
            $index = 0;
            while ($row = mysql_fetch_assoc($this->result)) {
                    $resultArray[$index] = $row[$fieldName];
                    $index += 1;
            }
            return $resultArray;
	}

        /**
         *
         * @return <type> 
         */
        function getResultArray() 
        {
            $resultArray = array($this->getRowsCount());
            $index = 0;
            while($row = mysql_fetch_assoc($this->result)) {
                    $resultArray[$index] = $row;
                    $index += 1;
            }
            return $resultArray;
	}
	/**
         *
         * 
         */
	function close()
        {
            mysql_close($this->conn);
	}

}
?>